﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace SdmxMl.Data
{
    public class CsvProperties
    {
        #region CSV

        /// <summary>The delimiter character separating each field</summary>
        public char Delimiter { get; set; }

        /// <summary>The quotation character wrapping every field</summary>
        public char Quote { get; set; }

        /// <summary>The comment character indicating that a line is commented out (default is '#')</summary>
        public char CommentMarker { get; set; }

        #endregion

        #region Chunk


        /// <summary>The file size required for chunk mechanism</summary>
        public int HugeSize
        {
            get { return 1000 * 1024 * Huge_MB; }
        }

        /// <summary>The file size required for a chunk </summary>
        public int ChunkSize
        {
            get { return 1024 * ChunkSize_KB; }
        }

        /// <summary>chunk size in KB</summary>
        public int ChunkSize_KB { get; set; }

        /// <summary>Huge file detection limit in MB</summary>
        public int Huge_MB { get; set; }

        /// <summary>Default constructor</summary>
        public CsvProperties()
        {
            Delimiter = ';';
            Quote = '"';
            CommentMarker = '#';
            ChunkSize_KB = 1000;
            Huge_MB = 10;

        }

        #endregion

        /// <summary>
        /// Create an instance of this class (or derived) according to XML doc.
        /// </summary>
        /// <param name="reader"> a text reader instance. </param>
        /// <returns> the object instance constructed from xml document</returns>
        public CsvProperties XmlRead(string filepath)
        {
            TextReader reader = null;
            try
            {
                reader = new StreamReader(filepath);

                XmlSerializer xmlSer = new XmlSerializer(this.GetType());
                return xmlSer.Deserialize(reader) as CsvProperties;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to read. Invalid XML document", ex);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

        /// <summary>
        /// Xml serialization in specified filename
        /// </summary>
        /// <param name="xmlFile"> filename to create</param>
        public virtual void XmlWrite(string xmlFile)
        {
            StreamWriter sw = new StreamWriter(xmlFile);
            XmlWrite(sw);
        }

        /// <summary> XML Persistence using writer. </summary>
        /// <param name="tw"> an object derived from TextWriter</param>
        public virtual void XmlWrite(TextWriter tw)
        {
            try
            {
                XmlSerializer xmlSer = new XmlSerializer(this.GetType());
                xmlSer.Serialize(tw, this);
            }
            finally
            {
                tw.Close();
            }
        }

    }
}
